<?php 
namespace Home\Controller;
use \Think\Controller;

class InboxController extends HomeController
{
    public function index(){
        $list = D('Letter')->where(array('user_id'=>$_SESSION['home']['id']))->relation(true)->order('id desc')->select();
       // V($_POST);
        if (!empty($_POST)) {
            $lop['user_id']= $_SESSION['home']['id'];
            $lop['to_user_id']=$_POST['to_user_id'];
            $lop['content']=$_POST['content'];
            $lop['datetime']=date('Y-m-d H:i:s');
       // V($lop);
            $non=0;
            foreach ($lop as $val) {
                if ($val=='') {
                    $non = 1;
                }
            }
            if ($non==0) {
                $mop = M('letter')->add($lop);
            }
        }


// var_dump($_SESSION);
       $fu = M('letter')->field("u.name uname,u.id uid, u.image uimage,l.content lcon,l.datetime ldat,l.id lid")->table('zhihu_user u,zhihu_letter l')->where("u.id=l.user_id AND l.to_user_id=".$_SESSION['home']['id'])->order('l.id desc')->select();
       // var_dump($list);
       // var_dump($fu);
        $this->assign('fu',$fu);
        $this->assign('rows',$list);
        $this->assign('title','私信');
        $this->display('Inbox/index');
    }

    public function shanchu(){
        $run = M('letter')->where(array('id'=>$_GET['id']))->delete();

    }
    public function send(){
        $yonghu = $_POST['yonghu'];
        $neirong = $_POST['neirong'];
        $list = M('user')->where("`name`='{$yonghu}'")->select();

        // echo '123';

            // var_dump($list);
        if (!empty($list)) {
           $row = M('letter');
           $data = array();
           $data['to_user_id'] = $list[0]['id'];
           $data['user_id'] = $_SESSION['home']['id'];
            $data['content'] = $neirong;
            $data['datetime']=date('Y-m-d H:i:s');
            $result = $row->add($data);
            echo '1';
        } else {
            echo '0';
        }        
    }

}







